package com.animal.controller;

import com.alibaba.fastjson.JSONObject;
import com.animal.common.Constants;
import com.animal.common.Result;
import com.animal.entity.pojo.AAdopt;
import com.animal.entity.pojo.AAdoptt;
import com.animal.entity.pojo.AComment;
import com.animal.entity.pojo.APet;
import com.animal.service.AdoptService;
import com.animal.service.AdopttService;
import com.animal.service.CommentService;
import com.animal.service.PetService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/adopt")
public class AdoptController {
    @Resource
    private AdoptService adoptService;
    @Resource
    private PetService petService;
    @Resource
    private AdopttService adopttService;

    @Resource
    private CommentService commentService;

    @RequestMapping("/login")
    public String login(Model model) {
        List<AAdopt> list = adoptService.getAAdopAll();
        model.addAttribute("listdop", list);
        return "adopt";
    }


    @RequestMapping("getAdopAllByPetId")
    public String getAdopAllByPetId(@RequestParam(value = "petId") Integer petId, Model model) {
        APet aPet = petService.getPetById(petId);
        model.addAttribute("aPet", aPet);
        return "show";
    }

    @RequestMapping("/addAdoptt")
    public String addAdoptt(AAdoptt adoptt) {
        boolean flag = adopttService.addAdopttAll(adoptt);
        if (flag) {
            return "redirect:/adopt/login";
        } else {
            return "error";
        }
    }

    @RequestMapping("/commjson")
    @ResponseBody
    public String commjson(@RequestParam("replyName") String replyName,
                           @RequestParam("content") String content,
                           @RequestParam("petId") Integer petId) {
        AComment comment = new AComment();
        comment.setAuthor(replyName);
        comment.setComment(content);
        comment.setPetId(petId);
        int count = commentService.insertComment(comment);
        if (count > 0) {
            return "添加成功";
        } else {
            return "添加失败";
        }
    }

}
